<?php
require_once("includes/header.php");
require_once("includes/Paginator.php");
//require_once("includes/left_panel.php");
$itemsPerPageReviews = 10;
$currentPageReviews = 1;
$fieldname = 'SR_ID';
$orderby = 'asc';
$searchdata = '';
$link_page = '';

if (array_key_exists('search_request', $_POST)) {
    $searchdata = " and Prob_Desc like '%" . $_POST['search_request'] . "%' OR Status like '%" . $_POST['search_request'] . "%' OR
        service_request_number like '%" . $_POST['search_request'] . "%'";
}

if (array_key_exists('pageno', $_GET)) {
    if ($_GET['pageno']) {
        $currentPageReviews = $_GET['pageno'];
    }
}
if (array_key_exists('orderby', $_GET)) {
    if ($_GET['orderby']) {
        $orderby = $_GET['orderby'];
        $link_page = "&orderby=" . $orderby . "";
    }
}
if (array_key_exists('field', $_GET)) {
    if ($_GET['field']) {
        $fieldname = $_GET['field'];
        $link_page .= "&field=" . $_GET['field'] . "";
    }
}
$offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
$endlimit = $offset + $itemsPerPageReviews;

$store_id = $_GET['store_id'];


//location query

$location_query = " SELECT * ,[location].[Name] as location_name
                    FROM [location]
                    INNER JOIN [customer]
                    ON [location].[CustomerID]= [customer].[ID]
                    where [location].[Store_Id] = '" . $store_id . "'";
$location_result = $DBOBJ->select($location_query);

//store query
$store_query = "SELECT [Store_ID],[Store_Name],[Store_Info]
                FROM [t_Stores]
                where [Store_ID] = '" . $store_id . "'";
$store_result = $DBOBJ->select($store_query);




$request_count_query = "SELECT  [SR_ID]
                        ,[Store_ID]
                        ,[Prob_Desc]
                        ,[Status]
                        ,[Submitted_On]
                        ,[Location_id]
                        ,[service_request_number]
                    FROM [t_ServiceRequests]
                    where [Store_ID] = '" . $store_id . "' " . $searchdata . "";
$request_count_result = $DBOBJ->select($request_count_query);





$request_query = "SELECT  * FROM (
                            SELECT *,ROW_NUMBER() OVER (ORDER BY " . $fieldname . " " . $orderby . ") AS RN
                    FROM [t_ServiceRequests]  
                    where [Store_ID] = '" . $store_id . "' " . $searchdata . "  ) AS X
                    where                  
                    RN > " . $offset . "
                    AND RN <= " . $endlimit . "";
$request_result = $DBOBJ->select($request_query);

if (count($request_result) > 0) {
    $paginator = new GTL_Paginator();
    $paginator->setItemsPerPage($itemsPerPageReviews);
    $paginator->setItemsTotal(count($request_count_result));
    $paginator->setCurrentPage($currentPageReviews);
    $link = $_SESSION['APP_FOLDER'] . '/location.php?store_id=' . $store_id . '&pageno=PAGENO' . $link_page;
    $paginator->paginate($link);
}

$locationData = "";
foreach ($location_result as $loc_value) {
    $locationData .="['<a href=\"location.php?store_id=" . $store_result[0]['Store_ID'] . "\">" . $store_result[0]['Store_Name'] . "</a><br>" . $loc_value['Address1'] . ", " . $loc_value['City'] . ", " . $loc_value['Province'] . "'," . $loc_value['Lattitude'] . ", " . $loc_value['Longitude'] . "],";
}
$finalLocationData = rtrim($locationData, ",");
?>

<script src="http://maps.google.com/maps/api/js?sensor=false" 
type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function() {
        var locations = [
<?php echo $finalLocationData; ?>
        ];

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            //center: new google.maps.LatLng(-33.92, 151.25),
            center: new google.maps.LatLng(28.5267266, -81.4648804),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    });
</script>



<!-- main container -->
<div class="content">

 

    <div class="container-fluid">
        <div id="pad-wrapper">


            <div class="map-wrapper map section2">
                <div class="row-fluid head">
                    <div class="span12 map_left_container">

                        <div class="left_title span12"> <h4> Location Info  </h4> </div>

                        <div class="left_info_container span12 map_span"> 
                            <div class="left_info_left span7 "> 
                                <span> 7-eleven </span>
                                <span> <?php echo $location_result[0]['Address1'];?> </span>
                                <span>  <?php echo $location_result[0]['City'];?>, <?php echo $location_result[0]['Zip'];?> </span>
                                <span> open today <a href="#" > 24 hours </a> </span>
                            </div>
                            <div class="left_info_right span5"> 
                                <div class="left_info_right_top span12"> 
                                    <span class="icon_direction" > direction </span>
                                    <span class="spacer" >   </span>
                                    <span class="icon_save" > save </span>
                                </div>
                                <div class="left_info_right_bottom span12"> 
                                    <input type="button" class="mapnewbtn" value="NEW" /> 
                                </div>
                            </div>
                        </div>

                        <div class="left_map_image span12">  
                            <div id="map" style="width: 400px; height: 268px;"></div>
                        </div>


                        <div class="left_map_content span12 map_span">

                            <div class="left_map_content_title  ">
                                <span class="floatleft" >
                                    Open Since
                                </span>

                                <span class="floatright" >
                                    <?php 
                                $date_string = date_format(  $location_result[0]['CreateDate'], 'F Y' );
                                echo $date_string;
                                ?>
                                </span>
                            </div>


                            <div class="left_map_sub_content span12" >
                                <div class="left_map_sub_content_row  " >
                                    <span class="title" >
                                        Manager:
                                    </span>
                                    <span class="value" >
                                        <?php echo ucfirst($location_result[0]['Name']);?>
                                    </span>
                                </div>
                                <div class="left_map_sub_content_row  " >
                                    <span class="title">
                                        Phone:
                                    </span>
                                    <span class="value">
                                       <?php echo $location_result[0]['Phone1'];?>
                                    </span>
                                </div>
                                <div class="left_map_sub_content_row  " >
                                    <span class="title">
                                        Cell:
                                    </span>
                                    <span class="value">
                                       <?php echo $location_result[0]['Phone2'];?>
                                    </span>
                                </div>

                                <div class="left_map_sub_content_row  " >
                                    <span class="title">
                                        Email:
                                    </span>
                                    <span class="value">
                                        <?php echo $location_result[0]['Email1'];?>
                                    </span>
                                </div>
                            </div>


                           <!-- <div class="left_map_content_employ  ">
                                <span class="emplopy_title">
                                    employees:
                                </span>

                                <ul class="">
                                    <li> <img src="img/map1/location_info_21.png" alt="map_image" />  </li>
                                    <li> <img src="img/map1/location_info_23.png" alt="map_image" />  </li>
                                    <li> <img src="img/map1/location_info_21.png" alt="map_image" />  </li>
                                    <li> <img src="img/map1/location_info_23.png" alt="map_image" />  </li>
                                    <li> <img src="img/map1/location_info_21.png" alt="map_image" />  </li>
                                    <li> <img src="img/map1/location_info_23.png" alt="map_image" />  </li>
                                </ul>

                            </div>-->


                        </div>



                    </div>
                </div>
            </div>    
            <!-- products table-->
            <!-- the script for the toggle all checkboxes from header is located in js/theme.js -->
            <div class="table-wrapper products-table section map-content map-content2">
                
                 <div class="bred_crum map_span">
                    Dashboard &nbsp; \ &nbsp;
                    7-eleven &nbsp; \ &nbsp;
                   <?php echo $location_result[0]['location_name'];?> &nbsp; \ &nbsp;
                    <?php echo $store_result[0]['Store_Name'];?>
                </div>
                
                <div class="row-fluid head">
                    <div class="span12">
                        <h4>Facility Dashboard</h4>
                    </div>
                </div>

                <div class="row-fluid filter-block">
                    <div class="pull-right top_moduel_container">
                        <div class=" open_tickets top_moduel ">
                            <div class="top_moduel_title"> OPEN TICKETS </div>
                            <div class="top_moduel_number"> 8149 </div>
                            <div class="top_moduel_graph"> <img src="img/map/map_graph_1.png" alt="map_image" /> </div>
                        </div>

                        <div class="vendor_working top_moduel">
                            <div class="top_moduel_title"> VENDORS WORKING </div>
                            <div class="top_moduel_number">150</div>
                            <div class="top_moduel_graph"> <img src="img/map/map_graph_2.png" alt="map_image" /> </div>
                        </div>

                        <div class="locations top_moduel">
                            <div class="top_moduel_title"> LOCATIONS </div>
                            <div class="top_moduel_number"> 43,748 </div>
                            <div class="top_moduel_graph"> <img src="img/map/map_graph_3.png" alt="map_image" /> </div>

                        </div>
                    </div>
                </div>


                <div class="row-fluid filter-block">
                    <div class="map_graph_container">
                        <div class="map_graph">
                            <img src="img/map/map_graph.png" alt="map_image" /> 
                        </div>
                    </div>
                </div>

                 <div class="floatleft service_class">Service Requests</div>
                 &nbsp;&nbsp;
                <div class="dataTables_filter floatleft" id="example_filter">
                    <form method="post" name="search_form"> <label>Search: 
                        <input type="text" name="search_request" aria-controls="example" class="input-medium">
                    </label> </form>
                </div>
                
                <div class ="floatright">
                    <a href="request_form.php?store_id=<?php echo $store_id?>" class="btn btn-primary"> Add New </a>
                </div>
               
                <div class="row-fluid">
                    <table class="table table-hover map-table">
                        <thead>
                            <tr>
                                <th class="span3 name">
                                    Number  <div class="sort" >
                             <span class="asc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=service_request_number'"></span> 
                            <span class="desc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=service_request_number'"></span>
                        </div>
                            </th>
                            <th class="span3 name">
                                Date  <div class="sort" >
                                <span class="asc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=Submitted_On'"></span> 
                                <span class="desc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=Submitted_On'"></span>
                            </div>
                            </th>
                
                            <th class="span3 title">
                                Title <div class="sort" >
                                <span class="asc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=Prob_Desc'"></span> 
                                <span class="desc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=Prob_Desc'"></span>
                
                            </div>
                            </th>
                            <th class="span3 priority">
                                Priority <div class="sort" >
                                <span class="asc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=Status'"></span> 
                                <span class="desc" onclick="window.location = '?store_id=<?php echo $store_id?>&pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=Status'"></span></div>
                            </th>
                         </tr>
                        </thead>
                        <tbody>
                            <?php 
                     if(count($request_result) > 0):
                     foreach($request_result as $value):
                     ?>
                    <tr>
                        <td>
                            <a href="request_form.php?id=<?php echo $value['SR_ID']?>">
                                [#<?php echo $value['service_request_number'];?>]
                            </a>
                        </td>
                        <td>
                           <?php 
                            $date_string = date_format($value['Submitted_On'], 'F j,Y H:i' );
                                                           echo $date_string;
                           ?>
                        </td>
                        <td class="description">
                          <?php echo  substr($value['Prob_Desc'], 0, 25);?>
                        </td>
                        <td>
                            <?php 
                            $class = "";
                            if($value['Status'] == 'Active'):
                                            $class = 'label-success';
                                        endif;
                                        if($value['Status'] == 'Inactive'):
                                            $class = 'label-important';
                                        endif;
                                        if($value['Status'] == 'Progress'):
                                            $class = 'label-info';
                                        endif;
                                        if($value['Status'] == 'Dispatched'):
                                            $class = 'label-inverse';
                                        endif;
                                        if($value['Status'] == 'Done'):
                                            $class = 'label-success';
                                        endif;
                                        if($value['Status'] == 'Waiting for Info'):
                                            $class = 'label-info';
                                        endif;
                                        if($value['Status'] == 'Hold'):
                                            $class = 'label-warning';
                                        endif;
                                        
                                        
                                        ?>
                            <span class="label <?php echo $class;?>">#<?php echo $value['Status'];?></span>
                             &nbsp;&nbsp;
                            <a href="service_location.php?id=<?php echo $value['SR_ID'] ?>"> <span class="label label-info">View ServiceMan</span></a>
                        </td>
                    </tr>
                   <?php  endforeach;?>
                    <tr><td colspan="6" style="text-align: right"> <?php echo $paginator->displayPages();?></td></tr>
                   <?php  else:?>
                   <tr><td colspan="6"> No record Found</td></tr>
                   <?php endif;?>

                        </tbody>
                    </table>
                </div>
            </div>
            <!-- end products table -->


        </div>
    </div>
</div>
<!-- end main container -->

<?php
require_once("includes/footer.php");
?>